Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /** * Stripe Service - Payment Integration * Handles Stripe payment link creation and status checking * * @see https://stripe.com/docs/api */ const Stripe = require('stripe'); const { logger } = require('../config/logger'); class StripeService { constructor(secretKey) { if (!secretKey || secretKey.trim() === '') { throw new Error('Stripe secret key is required'); } this.stripe = new Stripe(secretKey); logger.info('StripeService initialized:', { hasKey: !!secretKey, keyPrefix: secretKey.substring(0, 7) }); } async createPayment(data) { try { // Create payment link const currency = (data.currency || 'brl').toString().toLowerCase(); const paymentLink = await this.stripe.paymentLinks.create({ line_items: [ { price_data: { currency, product_data: { name: data.description || 'Payment via WhatsApp', }, unit_amount: Math.round(data.amount * 100), // Amount in cents }, quantity: 1, }, ], after_completion: { type: 'redirect', redirect: { url: `${process.env.BASE_URL || 'http://localhost:3000'}/payments/success`, }, }, metadata: { reference_id: data.reference_id || `REF-${Date.now()}`, customer_name: data.customer_name || 'N/A', customer_phone: data.customer_phone || 'N/A', }, }); logger.info('Stripe payment link created:', { id: paymentLink.id, url: paymentLink.url, amount: data.amount }); return { success: true, payment_id: paymentLink.id, payment_url: paymentLink.url, status: 'pending' }; } catch (error) { logger.error('Error creating Stripe payment:', error); let errorMessage = 'Error creating Stripe payment'; if (error.type === 'StripeAuthenticationError') { errorMessage = 'Stripe authentication failed. Please verify your API key.'; } else if (error.message) { errorMessage = error.message; } return { success: false, error: errorMessage, details: error }; } } async getPaymentStatus(paymentLinkId) { try { // Get payment link const paymentLink = await this.stripe.paymentLinks.retrieve(paymentLinkId); // Check if there are any successful payments const sessions = await this.stripe.checkout.sessions.list({ payment_link: paymentLinkId, limit: 1, }); let status = 'pending'; let paid_at = null; if (sessions.data.length > 0) { const session = sessions.data[0]; if (session.payment_status === 'paid') { status = 'paid'; paid_at = new Date(session.created * 1000); } else if (session.status === 'expired') { status = 'cancelled'; } } return { success: true, status: status, paid_at: paid_at, stripe_status: paymentLink.active ? 'active' : 'inactive' }; } catch (error) { logger.error('Error checking Stripe status:', error); return { success: false, error: error.message || 'Error checking status', status: 'pending', paid_at: null }; } } } module.exports = StripeService; |